<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01.引出生命周期</title>
  </head>

  <body>
    <div id="app">
      <h2 :style="{opacity:opacity}">Vue学得烂怎么办？</h2>
      <button @click="death">不活了</button>
    </div>
  </body>
  <script src="../js/vue.js"></script>
  <script>
    Vue.config.productionTip = false;
    const vm = new Vue({
      el: '#app',
      data() {
        return {
          opacity: 0.5
        }
      },
      methods: {
        death() {
          // 销毁：vm还在，this不再管理data中的数据了，数据的监听也没了
          this.$destroy();
          console.log(this);
        }
      },
      // vue完成页面的解析，并把初始真实的DOM元素放入页面后调用mounted(挂载完毕)
      mounted() {
        //this就是vm实例对象
        console.log("vm管理app容器了", this);
        this.timer = setInterval(() => {
          console.log("定时器正在执行");
          this.opacity -= 0.1
          if (this.opacity <= 0) {
            this.opacity = 1
          }
        }, 1000);
      },
      //vm将要销毁
      beforeDestroy() {
        clearInterval(this.timer);
      }
    })
  </script>

</html>